﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Maticsoft.Common;

namespace Maticsoft.Web.Admin.Shopping
{
    public partial class EditBook : PageBase
    {
        string act = string.Empty;
        string id = string.Empty;

        BLL.Books bll = new BLL.Books();
        Model.Books model = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            Check("shopping", "modify");

            if (!string.IsNullOrEmpty(Request.QueryString["act"]))
            {
                act = Request.QueryString["act"];
            }
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                id = Request.QueryString["id"];
            }

            if (!IsPostBack)
            {
                LoadList();
                if (!string.IsNullOrEmpty(BindBookDiscount()))
                {
                    txtDiscount.Text = BindBookDiscount();
                }
                GetData();
            }
        }

        protected void GetData()
        {
            if (!string.IsNullOrEmpty(id) && act=="edit")
            {
                model = bll.GetModel(int.Parse(id));

                BLL.Channel channelBll = new BLL.Channel();
                Model.Channel channelModel = channelBll.GetModel(int.Parse(model.CategoryId.ToString()));
                ddlType.SelectedValue = model.CategoryId.ToString();
                txtName.Text = model.BookName;
                txtISBN.Text = model.ISBN;
                txtPrice.Text = decimal.Parse(model.MarketPrice.ToString()).ToString("f2");
                txtDiscount.Text = decimal.Parse(model.SalePrice.ToString()).ToString("f2");
                txtAuthor.Text = model.author;
                txtPublishDate.Text = model.PublicDate;
                txtPulishCount.Text = model.PublishCount.ToString();
                txtPages.Text = model.Page.ToString();
                txtSize.Text = model.BookSize;
                FCKeditor2.Value = model.Detail;
                FCKeditor3.Value = model.BookChapter;
                if (!string.IsNullOrEmpty(model.Pic))
                {
                    Image1.ImageUrl = "~/uploadpic/" + model.Pic;
                }
                cbHot.Checked = model.IsHot == 1 ? true : false;
                cbGood.Checked = model.IsGood == 1 ? true : false;
                cbHotSale.Checked = model.IsHotSale == 1 ? true : false;
                cbTop.Checked = model.IsTop == 1 ? true : false;
                txtLayOut.Text = model.Layout;
            }
        }

        /// <summary>
        /// 加载整个分类表
        /// </summary>
        private void LoadList()
        {
            int MTcount = 0;//执行遍历的次数
            //开始迭代加载
            MakeTr("0", MTcount);

        }
        /// <summary>
        /// 装配每一行
        /// </summary>
        /// <param name="id">上级ID</param>
        /// <param name="count">迭代次数</param>
        private void MakeTr(string id, int count)
        {
            BLL.Channel chaBLL = new BLL.Channel();
            DataView dv = new DataView(chaBLL.GetList("parentID=" + id + " order by ChannelId asc").Tables[0]);
            foreach (DataRowView drv in dv)
            {
                //形成下拉列表
                ListItem li = new ListItem(MakeFelgefu(count) + drv["ChannelName"].ToString(), drv["ChannelId"].ToString());
                ddlType.Items.Add(li);

                //再次遍历
                MakeTr(drv["ChannelId"].ToString(), count + 1);
            }

        }
        /// <summary>
        /// Make分隔符
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        private string MakeFelgefu(int count)
        {
            string Returnwords = string.Empty;
            if (count == 0)
            {
                Returnwords = "";
            }

            else
            {
                Returnwords = ("┠").PadLeft(count, '　');
            }
            return Returnwords;
        }

        protected string BindBookDiscount()
        {
            BLL.WebSiteConfig configBll = new BLL.WebSiteConfig();
            return Convert.ToString(configBll.GetModel(1).ext1);
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (act == "add")
            {
                if (CheckNumber(txtISBN.Text.Trim()))
                {
                    model = new Model.Books();
                    model.BookName = txtName.Text.Trim();
                    model.ISBN = txtISBN.Text.Trim();
                    model.MarketPrice = decimal.Parse(txtPrice.Text.Trim());
                    model.SalePrice = decimal.Parse(txtDiscount.Text.Trim());
                    model.author = txtAuthor.Text.Trim();
                    model.PublicDate = txtPublishDate.Text.Trim();
                    model.PublishCount = txtPulishCount.Text.Trim();
                    model.Page = int.Parse(txtPages.Text.Trim());
                    model.BookSize = txtSize.Text.Trim();
                    model.CategoryId = int.Parse(ddlType.SelectedValue);
                    model.Detail = FCKeditor2.Value;
                    model.BookChapter = FCKeditor3.Value;
                    model.IsHot = cbHot.Checked == true ? 1 : 0;
                    model.IsGood = cbGood.Checked == true ? 1 : 0;
                    model.IsHotSale = cbHotSale.Checked == true ? 1 : 0;
                    model.IsTop = cbTop.Checked == true ? 1 : 0;
                    model.Layout = txtLayOut.Text.Trim();

                    model.CreateTime = DateTime.Now;
                    model.UpdateTime = DateTime.Now;
                    model.IsActive = 1;
                    model.Hits = 0;
                    model.OrderNo = 0;
                    model.SaleNum = 0;
                    model.CommentNum = 0;
                    ImageUpload img = new ImageUpload();
                    img.SavePath = "~/uploadpic/";
                    img.FormFile = File1;
                    img.Upload();
                    if (img.Error == 0)
                    {
                        model.Pic = img.OutFileName;
                    }

                    int nId = bll.Add(model);

                    MessageBox.ShowAndRedirect(this.Page, "添加成功", "books.aspx");
                }
                else
                {
                    MessageBox.Show(this.Page, "编号重复");
                }
            }
            if (act == "edit" && !string.IsNullOrEmpty(id))
            {
                model = bll.GetModel(int.Parse(id));
                model.BookName = txtName.Text.Trim();
                model.ISBN = txtISBN.Text.Trim();
                model.MarketPrice = decimal.Parse(txtPrice.Text.Trim());
                model.SalePrice = decimal.Parse(txtDiscount.Text.Trim());
                model.author = txtAuthor.Text.Trim();
                model.PublicDate = txtPublishDate.Text.Trim();
                model.PublishCount = txtPulishCount.Text.Trim();
                model.Page = int.Parse(txtPages.Text.Trim());
                model.BookSize = txtSize.Text.Trim();
                model.CategoryId = int.Parse(ddlType.SelectedValue);
                model.Detail = FCKeditor2.Value;
                model.BookChapter = FCKeditor3.Value;
                model.IsHot = cbHot.Checked == true ? 1 : 0;
                model.IsGood = cbGood.Checked == true ? 1 : 0;
                model.IsHotSale = cbHotSale.Checked == true ? 1 : 0;
                model.IsTop = cbTop.Checked == true ? 1 : 0;
                model.Layout = txtLayOut.Text.Trim();
                ImageUpload img = new ImageUpload();
                img.SavePath = "~/uploadpic/";
                img.FormFile = File1;
                img.Upload();
                if (img.Error == 0)
                {
                    model.Pic = img.OutFileName;
                }

                model.UpdateTime = DateTime.Now;

                bll.Update(model);
                

                MessageBox.ShowAndRedirect(this.Page, "修改成功", "books.aspx");
            }
        }

        protected bool CheckNumber(string number)
        {
            DataSet ds = bll.GetList("ISBN='" + number + "'");
            if (ds.Tables[0].Rows.Count > 0)
            {
                return false;
            }

            return true;
        }
    }
}